package com.goscom.app.deeset;

import java.util.ArrayList;
import java.util.Calendar;
import com.goscom.app.database.DBAdapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AddContracted extends Activity {

	private String strMerId = "";
	private String strAct = "";
	private String strContractedId = "";
	private String strSearch = "";
	private DBAdapter mDB;
	private EditText mEdtMon, mEdtTue, mEdtWed, mEdtThu, mEdtFri, mEdtSat,
			mEdtSun;
	private Spinner mSpinSto, mSpinJob;
	private TextView txtTit;
	private int intStoRel=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcontracted);

		mEdtMon = (EditText) findViewById(R.id.edtAddStoMon);
		mEdtTue = (EditText) findViewById(R.id.edtAddStoTue);
		mEdtWed = (EditText) findViewById(R.id.edtAddStoWed);
		mEdtThu = (EditText) findViewById(R.id.edtAddStoThu);
		mEdtFri = (EditText) findViewById(R.id.edtAddStoFri);
		mEdtSat = (EditText) findViewById(R.id.edtAddStoSat);
		mEdtSun = (EditText) findViewById(R.id.edtAddStoSun);
		mSpinSto = (Spinner) findViewById(R.id.spinAddStoStoRel);
		mSpinJob = (Spinner) findViewById(R.id.spinAddStoJobSkill);
		txtTit = (TextView) findViewById(R.id.txtAddStoTit);

		mDB = new DBAdapter(AddContracted.this);
		mDB.open();

		getMerId();

		loadStoreAndRetailer();
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
			strAct = getIntent().getStringExtra("activity");
			strSearch = getIntent().getStringExtra("search");
			strContractedId = getIntent().getStringExtra("contractedid");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId != null) {
			ContentValues content = new ContentValues();
			content = mDB.queryDataByMerContracted("CONTRACTEDHOURS", strMerId,
					strContractedId);
			if (content != null) {
				mEdtMon.setText(content.getAsString("mon"));
				mEdtTue.setText(content.getAsString("tue"));
				mEdtWed.setText(content.getAsString("wed"));
				mEdtThu.setText(content.getAsString("thu"));
				mEdtFri.setText(content.getAsString("fri"));
				mEdtSat.setText(content.getAsString("sat"));
				mEdtSun.setText(content.getAsString("sun"));
				intStoRel=Integer.valueOf(content
						.getAsString("intstorel"));
				
				mSpinJob.setSelection(Integer.valueOf(content
						.getAsString("intjobskill")));
				strSearch = content.getAsString("search");
			}
			if (strAct.equals("contracted")) {
				txtTit.setText("Add Store for Contracted Hours");
			} else {
				txtTit.setText("Add Store for Non Contracted Hours");
			}
		}
	}

	public void loadStoreAndRetailer() {
		ArrayList<String> arrStore = new ArrayList<String>();
		arrStore = mDB.queryListStoreAndRetailer(strSearch);
		if (arrStore.size() != 0) {
			ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, arrStore);
			dataAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			mSpinSto.setAdapter(dataAdapter);
			mSpinSto.setSelection(intStoRel);
		}
	}

	public void saveStore(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(AddContracted.this, ContractedHours.class)
				.putExtra("merid", strMerId));
	}

	public void exit(View v) {
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(AddContracted.this, ContractedHours.class)
				.putExtra("merid", strMerId));
	}

	public void saveDB() {
		if (mDB.queryMerchandiserContracted("CONTRACTEDHOURS", strMerId,
				strContractedId) == 0) {
			insertDB();
		} else {
			mDB.deleteDataContracted("CONTRACTEDHOURS", strMerId,
					strContractedId);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("contractedid",
				String.valueOf(Calendar.getInstance().getTimeInMillis()));
		content.put("storel", String.valueOf(mSpinSto.getSelectedItem()));
		content.put("jobskill", String.valueOf(mSpinJob.getSelectedItem()));
		content.put("intstorel", mSpinSto.getSelectedItemPosition());
		content.put("intjobskill", mSpinJob.getSelectedItemPosition());
		content.put("mon", mEdtMon.getText().toString());
		content.put("tue", mEdtTue.getText().toString());
		content.put("wed", mEdtWed.getText().toString());
		content.put("thu", mEdtThu.getText().toString());
		content.put("fri", mEdtFri.getText().toString());
		content.put("sat", mEdtSat.getText().toString());
		content.put("sun", mEdtSun.getText().toString());
		content.put("activity", strAct);
		content.put("search", strSearch);
		mDB.insertData("CONTRACTEDHOURS", content);
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}
}
